<div class="section" id="sitemaplist">

	<div class="section-title">Import Voucher</div>
    
    <div class="section-content padding1">
    
    	<form id="frm" name="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
        	<input type="hidden" id="orderid" name="orderid" value="<?php echo $order['orderid']?>" />
        	<div class="button right">
            	<a class="button" onclick="saveImport(false)">Save</a>
                <a class="button" onclick="saveImport(true)"><?php echo $this->document->text['button_saveprint']?></a>
     	        <a class="button" href="index.php?route=addon/import"><?php echo $this->document->text['button_cancel']?></a>
                
            </div>
            <div class="clearer">&nbsp;</div>
        
        	<div>
            	<p>
            		<label>Created by:&nbsp;&nbsp;</label>
            	    <input type="text" class="text" size="40" id="createby" name="createby" value="<?php echo $order['createby']?>" />
            	(*)</p>
          <p>
            		<label><?php echo $this->document->text['text_supplier']?>:&nbsp;&nbsp;</label>
                    <textarea id="supplier" name="supplier"><?php echo $order['supplier']?></textarea>
(*)					
            	</p>
                <p>
            		<label><?php echo $this->document->text['text_date']?>:&nbsp;&nbsp;</label>
<script language="javascript">
 	$(function() {
		$("#orderdate").datepicker({
				changeMonth: true,
				changeYear: true,
				dateFormat: 'dd-mm-yy'
				});
		});
 </script>
                    <input type="text" class="text" id="orderdate" name="orderdate" value="<?php echo $order['orderdate']?>" />
(*)            	</p>
                <p>
            		<label>Comment:&nbsp;&nbsp;</label><br />
                    <textarea class="textarea2" id="comment" name="comment" cols="50"><?php echo $order['comment']?></textarea>
            	</p>
            </div>
      <div>
            	<p>
                <input type="hidden" id="listproductid" name="listproductid" />
                
                <a class="button" onclick="browsProduct()">Add product item</a></p>
               <!-- <a class="button" onclick="addRow('','','',0,0,0,index);index++">Add product item</a></p>-->
            	<table class="data-table">
                	<thead>
                    	<tr>
                            
                            <th align="left"><?php echo $this->document->text['text_product']?></th>
                            <th align="right">Instock</th>
                            <th align="right">Quantity</th>
                            <th align="right">Cost</th>
                            
                            <th align="right">Sub total</th>
                            <th>&nbsp;</th>
                        </tr>
                    </thead>
                    <tbody id="bodylist">
               		
                    </tbody>
                    <tfoot>
                    	<tr>
                        	
                            <th></th>
                            <th></th>
                            <th></th>
                            <th align="right">Total</th>
                            <th align="right" id="total"></th>
                            <th></th>
                        </tr>
                    </tfoot>
                </table>
            </div>
            <div id="listremove">
            </div>
        </form>
    
    </div>
    
</div>
<div style="display:none">
<?php
	foreach($medias as $item)
    {
?>
		<div id="media<?php echo $item['mediaid']?>"><?php echo $item['title']?></div>
        <div id="instock<?php echo $item['mediaid']?>"><?php echo $item['instock']?></div>
<?php
    }
?>
</div>
<script language="javascript" src="<?php echo DIR_JS?>grid.js"></script>
<script language="javascript">
var index = <?php echo count($detail)?>;


function saveImport(pri)
{
	var oEditor = CKEDITOR.instances['supplier'] ;
	var pageValue = oEditor.getData();
	$('textarea#supplier').val(pageValue);
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	$.post("index.php?route=addon/import/savepost", 
		   $("#frm").serialize(),
		   function(data)
		   {
				/*if(data=="true")
					window.location = '?route=addon/import';
				else
				{
					alert("Error! Please check your input data!");
					$.unblockUI(); 
				}*/
				if(data=="false")
				{
					alert("Error! Please check your input data!");
					$.unblockUI(); 
				}
				else
				{
					if(pri == true)
					{
						//printVoucher(data);
						openDialog('?route=addon/import/view&orderid='+data,800,500);
						window.location = '?route=addon/import';
						
					}
					else
						window.location = '?route=addon/import';
					
				}
			}
		   );
		
	
}

function getPrice(productid,pos)
{
	/*$.getJSON("?route=common/product/getProduct&productid="+productid, 
			function(data) 
			{
				//alert(data.subimage.imagepreview);
				$("#price"+pos).val(data.media.price)
				getSubTotal(pos);
			});*/
	
}



function addRow(id,productid,productname,quantity,price,discount,pos)
{
	//alert(quantity*price)
	subt=quantity*price*(1-discount/100)
	id='<input type="hidden" id="id'+pos+'" name="id['+pos+']" value="'+id+'" />';
	proid='<input type="hidden" id="product'+pos+'" name="product['+pos+']" value="'+productid+'" />';
	name  = '<td >'+productname+'</td>';
	instock = '<td align="right">'+$('#instock'+productid).html()+'</td>';
	quantity = '<td align="right"><input type="text"  class="text number" style="text-align:right" id="quantity'+pos+'" name="quantity['+pos+']" value="'+formateNumber(quantity)+'" onkeyup="getSubTotal('+pos+')" size="1"/></td>';
	cost 	 = '<td align="right"><input type="text" class="text number" style="text-align:right" id="price'+pos+'" name="price['+pos+']" value="'+formateNumber(price)+'" onkeyup="getSubTotal('+pos+')" size="14"/></td>';
	discount = '<input type="hidden" class="text" style="text-align:right" id="discount'+pos+'" name="discount['+pos+']" value="'+discount+'" onkeyup="getSubTotal('+pos+')" size="1"/>';
	subtotal = '<td id="subtotal'+pos+'" align="right">'+ numberView(subt) +'</td>';
	control  = '<td><a class="button" onclick="removeRow('+pos+')">Remove</a></td>';
	row = "<tr id=row"+pos+">"+id+proid+name+instock+quantity+cost+discount+subtotal+control+"</tr>";
	$("#bodylist").append(row);
	//document.getElementById("bodylist").innerHTML+=row;
	/*if(catagoryid=="")
		$("#category"+pos).load("?route=common/combobox/comboboxCategory&select=&index="+pos);*/
	//$("#category"+pos).html(comboboxCategory(catagoryid,pos));
	//selectCategory(catagoryid,productid,pos)
	getTotal();
	$(".number").keyup(function (e)
		{
			//alert(formateNumber($(this).val()))
			num = formateNumber($(this).val().replace(/,/g,""));
			$(this).val(num)
		});
	$(".number").keypress(function (e)
		{
			
		  return isNumber(e.which);
		});
	
}

function browsProduct()
{
	openDialog("?route=soft/product/browse&sitemapid=category",800,500);
	
	listproductid = $("#listproductid").val().split(",");
	for(j=0;j < listproductid.length;j++)
	{
		if(listproductid[j]!="")
		{
			
			addRow('',listproductid[j],$("#media"+listproductid[j]).html(),0,0,0,index++);
		}
	}
	$("#listproductid").val("");
}

$(document).ready(function() {
	setCKEditorType('supplier',4);
<?php
	if(count($detail))
	{
		foreach($detail as $key => $item)
		{
?>
		
		addRow("<?php echo $item['id']?>","<?php echo $item['mediaid']?>",$("#media<?php echo $item['mediaid']?>").html(),"<?php echo $item['quantity']?>","<?php echo $item['price']?>","<?php echo $item['discount']?>","<?php echo $key?>");
		//setTimeout('selectCategory("<?php echo $item['refersitemap']?>","<?php echo $item['mediaid']?>","<?php echo $key?>")',1000);
<?php
		}
		//getTotal();
	}
?>

	// Handler for .ready() called.
	/*<?php
	if(count($detail))
		foreach($detail as $key => $item)
		{
?>
			$.blockUI({ message: "<h1>Please wait...</h1>" }); 
			$("#category<?php echo $key?>").load("?route=common/combobox/comboboxCategory&select=<?php echo $item['refersitemap']?>&index=<?php echo $key?>",
								  function()
								  {
										selectCategory("<?php echo $item['refersitemap']?>","<?php echo $item['mediaid']?>","<?php echo $key?>"); 
								  }
							  );
			
<?php
		}
?>
	*/
	
	
	
});



</script>
